package jehc.djshi.sys.web;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jehc.djshi.common.annotation.Auth;
import jehc.djshi.common.base.*;
import jehc.djshi.common.entity.OauthAccountEntity;
import jehc.djshi.common.util.JsonUtil;
import jehc.djshi.common.util.StringUtil;
import jehc.djshi.sys.model.XtDepartInfo;
import jehc.djshi.sys.service.XtDepartInfoService;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.github.pagehelper.PageInfo;
/**
 * @Desc 部门信息
 * @Author 邓纯杰
 * @CreateTime 2012-12-12 12:12:12
 */
@RestController
@RequestMapping("/xtDepartinfo")
@Api(value = "部门信息API",tags = "部门信息API",description = "部门信息API")
public class XtDepartInfoController extends BaseAction {

	@Resource
	private XtDepartInfoService xtDepartinfoService;

	/**
	* 查询部门信息列表并分页
	* @param baseSearch
	*/
	@ApiOperation(value="查询部门信息列表并分页", notes="查询部门信息列表并分页")
	@PostMapping(value="/list")
	@Auth(value = "/xtDepartInfo/list",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	public BasePage<List<XtDepartInfo>> getXtDepartInfoListByCondition(@RequestBody(required=true)BaseSearch baseSearch){

		Map<String, Object> condition = baseSearch.convert();

		commonHPager(baseSearch);

		List<XtDepartInfo> xtDepartInfoList = xtDepartinfoService.getXtDepartInfoListByCondition(condition);

		for(XtDepartInfo xtDepartInfo:xtDepartInfoList){

			if(!StringUtil.isEmpty(xtDepartInfo.getCreateId())){
				OauthAccountEntity createBy = getAccount(xtDepartInfo.getCreateId());
				if(null != createBy){
					xtDepartInfo.setCreateBy(createBy.getName());
				}
			}

			if(!StringUtil.isEmpty(xtDepartInfo.getUpdateId())){
				OauthAccountEntity modifiedBy = getAccount(xtDepartInfo.getUpdateId());
				if(null != modifiedBy){
					xtDepartInfo.setModifiedBy(modifiedBy.getName());
				}
			}
		}
		PageInfo<XtDepartInfo> page = new PageInfo<XtDepartInfo>(xtDepartInfoList);
		return outPageBootStr(page,baseSearch);
	}

	/**
	* 查询单个部门信息
	* @param id
	*/
	@ApiOperation(value="查询单个部门信息", notes="查询单个部门信息")
	@GetMapping(value="/get/{id}")
	@Auth(value = "/xtDepartinfo/get",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	public BaseResult<XtDepartInfo> getXtDepartInfoById(@PathVariable("id")String id){

		XtDepartInfo xtDepartInfo = xtDepartinfoService.getXtDepartInfoById(id);

		if(!StringUtil.isEmpty(xtDepartInfo.getCreateId())){

			OauthAccountEntity createBy = getAccount(xtDepartInfo.getCreateId());

			if(null != createBy){
				xtDepartInfo.setCreateBy(createBy.getName());
			}

		}
		if(!StringUtil.isEmpty(xtDepartInfo.getUpdateId())){
			OauthAccountEntity modifiedBy = getAccount(xtDepartInfo.getUpdateId());
			if(null != modifiedBy){
				xtDepartInfo.setModifiedBy(modifiedBy.getName());
			}
		}
		return outDataStr(xtDepartInfo);
	}

	/**
	 *一级目录
	 * @return
	 */
	@ApiOperation(value="一级目录", notes="一级目录")
	@GetMapping(value="/one/level/lists")
	@Auth(value = "/xtDepartinfo/one/level/lists",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	public BaseResult<List<XtDepartInfo>> lists(){
		List<XtDepartInfo> xtDepartInfoList = xtDepartinfoService.getXtDepartInfoList();
		return outDataStr(xtDepartInfoList);
	}

	/**
	 * 子级目录
	 * @param id
	 * @return
	 */
	@ApiOperation(value="子级目录", notes="子级目录")
	@GetMapping(value="/child/lists/{id}")
	@Auth(value = "/xtDepartinfo/child/lists",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	public BaseResult<List<XtDepartInfo>> childLists(@PathVariable("id")String id){
		Map<String,Object> condition = new HashMap<>();
		condition.put("parentId",id);
		List<XtDepartInfo> xtDepartInfoList = xtDepartinfoService.getXtDepartInfoListChild(condition);
		return outDataStr(xtDepartInfoList);
	}

	/**
	* 添加
	* @param xtDepartInfo
	*/
	@PostMapping(value="/add")
	@ApiOperation(value="创建单个部门信息", notes="创建单个部门信息")
	public BaseResult addXtDepartInfo(@RequestBody XtDepartInfo xtDepartInfo){
		int i = 0;
		if(null != xtDepartInfo){
			xtDepartInfo.setId(toUUID());
			if(StringUtil.isEmpty(xtDepartInfo.getParentId())){
				xtDepartInfo.setParentId("0");
			}
			i=xtDepartinfoService.addXtDepartInfo(xtDepartInfo);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}

	/**
	* 修改
	* @param xtDepartInfo
	*/
	@PutMapping(value="/update")
	@ApiOperation(value="编辑单个部门信息", notes="编辑单个部门信息")
	public BaseResult updateXtDepartinfo(@RequestBody XtDepartInfo xtDepartInfo){
		int i = 0;
		if(null != xtDepartInfo){
			if(StringUtil.isEmpty(xtDepartInfo.getParentId())){
				xtDepartInfo.setParentId("0");
			}
			i=xtDepartinfoService.updateXtDepartInfo(xtDepartInfo);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}

	/**
	* 删除
	* @param id
	*/
	@DeleteMapping(value="/delete")
	@ApiOperation(value="删除部门信息", notes="删除部门信息")
	public BaseResult delXtDepartInfo(String id){
		int i = 0;
		if(!StringUtil.isEmpty(id)){
			Map<String, Object> condition = new HashMap<String, Object>();
			condition.put("id",id.split(","));
			i=xtDepartinfoService.delXtDepartInfo(condition);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}

	/**
	 * 读取部门树
	 * @param id
	 */
	@ApiOperation(value="根据Id查询部门树", notes="根据Id查询部门树")
	@GetMapping(value="/tree/{id}")
	@Auth(value = "/xtDepartinfo/tree",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	public BaseResult getXtDepartInfoTree(@PathVariable("id")String id){
		Map<String, Object> condition = new HashMap<String, Object>();
		List<BaseTreeGridEntity> list = new ArrayList<BaseTreeGridEntity>();
		List<XtDepartInfo> xtDepartInfoList = xtDepartinfoService.getXtDepartInfoListAll(condition);
		for(int i = 0; i < xtDepartInfoList.size(); i++){
			XtDepartInfo xtDepartinfo = xtDepartInfoList.get(i);
			BaseTreeGridEntity BaseTreeGridEntity = new BaseTreeGridEntity();
			BaseTreeGridEntity.setId(xtDepartinfo.getId());
			BaseTreeGridEntity.setPid(xtDepartinfo.getParentId());
			BaseTreeGridEntity.setText(xtDepartinfo.getName());
			BaseTreeGridEntity.setExpanded(true);
			BaseTreeGridEntity.setSingleClickExpand(true);
			BaseTreeGridEntity.setIcon("/deng/images/icons/target.png");
			list.add(BaseTreeGridEntity);
		}

		BaseTreeGridEntity baseTreeGridEntity = new BaseTreeGridEntity();
		List<BaseTreeGridEntity> baseTreeGridEntityList = baseTreeGridEntity.buildTree(list,"0");
		String json = JsonUtil.toFastJson(baseTreeGridEntityList);
		return outStr(json);
	}
	
	/**
	 * 根据各种情况查找集合不分页（流程设计器中处理组 发起组等使用）
	 * @param id
	 * @return
	 */
	@ApiOperation(value=" 根据各种情况查找集合不分页", notes=" 根据各种情况查找集合不分页")
	@GetMapping(value="/list/{id}")
	@Auth(value = "/xtDepartinfo/list",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	public BaseResult<List<XtDepartInfo>> queryXtDepartInfoList(@PathVariable("id")String id){
		List<XtDepartInfo> list = new ArrayList<XtDepartInfo>();
		if(!StringUtil.isEmpty(id)){
			Map<String, Object> condition = new HashMap<String, Object>();
			condition.put("id", id.split(","));
			list = xtDepartinfoService.queryXtDepartInfoList(condition);
		}
		return  outItemsStr(list);
	}

	/**
	 * 读取部门树（Bootstrap---ztree）
	 * @param request
	 */
	@ApiOperation(value=" 查询部门树（Btree）", notes="查询部门树（Btree）")
	@GetMapping(value="/bztree")
	@Auth(value = "/xtDepartinfo/bztree",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	public BaseResult getXtDepartInfoBZTree(HttpServletRequest request){
		Map<String, Object> condition = new HashMap<String, Object>();
		List<BaseZTreeEntity> list = new ArrayList<BaseZTreeEntity>();
		List<XtDepartInfo> xtDepartInfoList = xtDepartinfoService.getXtDepartInfoListAll(condition);
		for(int i = 0; i < xtDepartInfoList.size(); i++){
			XtDepartInfo xtDepartInfo = xtDepartInfoList.get(i);
			BaseZTreeEntity BaseZTreeEntity = new BaseZTreeEntity();
			BaseZTreeEntity.setId(xtDepartInfo.getId());
			BaseZTreeEntity.setPId(xtDepartInfo.getParentId());
			BaseZTreeEntity.setText(xtDepartInfo.getName());
			BaseZTreeEntity.setName(xtDepartInfo.getName());
			BaseZTreeEntity.setExpanded(true);
			BaseZTreeEntity.setSingleClickExpand(true);
			list.add(BaseZTreeEntity);
		}
		BaseZTreeEntity baseZTreeEntity = new BaseZTreeEntity();
		List<BaseZTreeEntity> baseZTreeEntityList = baseZTreeEntity.buildTree(list,"0");
		String json = JsonUtil.toFastJson(baseZTreeEntityList);
		return outStr(json);
	}
}
